package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.jzfqwczzpfsn.service.IYwYnJzfqwczzPfsnQxService;
import com.augurit.gzcgwsp.jzfqwczzpfsn.service.IYwYnJzfqwczzPfsnService;
import com.augurit.gzcgwsp.jzfqwczzpfsn.service.IYwYnJzfqwczzPfsnbgQxService;
import com.augurit.gzcgwsp.jzfqwczzpfsn.service.IYwYnJzfqwczzPfsnbgService;
import com.augurit.gzcgwsp.jzfqwczzpfsn.service.IYwYnJzfqwczzPfsnyqQxService;
import com.augurit.gzcgwsp.jzfqwczzpfsn.service.IYwYnJzfqwczzPfsnyqService;
import com.augurit.gzcgwsp.jzfqwczzpfsn.web.form.YwYnJzfqwczzPfsnForm;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.readAndParseXML.service.IPfXzService;
import com.augurit.gzcgwsp.readAndParseXML.util.ConfigFjForPf;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;

/**
 * 排放 新增、变更、延期（由于方欣放在一个目录下，所以也合起来在这个类里面处理。变更和延期两个类暂时是没有用到的10-30）
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class PfXzServiceImpl implements IPfXzService{
	Logger logger = (Logger) LoggerFactory.getLogger(PfXzServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;

	@Autowired
	private IYwYnJzfqwczzPfsnService ywYnJzfqwczzPfsnService;
	@Autowired
	private IYwYnJzfqwczzPfsnbgService ywYnJzfqwczzPfsnbgService;
	@Autowired
	private IYwYnJzfqwczzPfsnyqService ywYnJzfqwczzPfsnyqService;
	@Autowired
	private IYwYnJzfqwczzPfsnQxService ywYnJzfqwczzPfsnQxService;
	@Autowired
	private IYwYnJzfqwczzPfsnbgQxService ywYnJzfqwczzPfsnbgQxService;
	@Autowired
	private IYwYnJzfqwczzPfsnyqQxService ywYnJzfqwczzPfsnyqQxService;
	//读取接口配置信息
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
	// xml存放路径
	private final String pfXzfilepath = localResource.getString("pfXzfilepath");

	// 附件存放路径
	private final String pfXzfujianpath = localResource.getString("pfXzfujianpath");

	// 解析成功反馈xml存放路径
	private final String pfXzfeedbackPath = localResource.getString("pfXzfeedbackPath");

	// 解析异常xml存放路径
	private final String pfXzexceptionPath = localResource.getString("pfXzexceptionPath");

	// 解析后，xml备份
	private final String pfXzbackPath = localResource.getString("pfXzbackPath");

	private Map<String, String> pfXzKeyMap;// xml内容的key

	private Map<String, String> pfXzKeyMapHead;// xml头部的key

	private Map<String, Long> fujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initpfXzMap() {
		// 初始化数据部分对应的map
		pfXzKeyMap = new HashMap<String, String>();
		pfXzKeyMap.put("approvalNo", "approvalNo");
		pfXzKeyMap.put("businessCode", "businessCode");
		pfXzKeyMap.put("YSL_NO", "YSL_NO");
		pfXzKeyMap.put("sendTime", "sendTime");
		
		pfXzKeyMap.put("GCDZ", "GCDZ");
		pfXzKeyMap.put("SQDW", "SQDW");
		pfXzKeyMap.put("GCMC", "GCMC");
		pfXzKeyMap.put("JSDWDZ", "JSDWDZ");
		pfXzKeyMap.put("JSDWLXR", "JSDWLXR");
		pfXzKeyMap.put("JSDWLXRLXDH", "JSDWLXRLXDH");
		pfXzKeyMap.put("SGDWGZ", "SGDWGZ");
		pfXzKeyMap.put("SGDWXMJL", "SGDWXMJL");
		pfXzKeyMap.put("SGDWXMJLLXDH", "SGDWXMJLLXDH");
		pfXzKeyMap.put("SGDWLXR", "SGDWLXR");
		pfXzKeyMap.put("SGDWLXRLXDH", "SGDWLXRLXDH");
		pfXzKeyMap.put("YSGS", "YSGS");
		pfXzKeyMap.put("YSGSLXR", "YSGS_LXR");
		pfXzKeyMap.put("YSGSLXRLXDH", "YSGSLXRLXDH");
		pfXzKeyMap.put("YSGSKGRQ", "YSGSKGRQ");
		pfXzKeyMap.put("YSGSWGRQ", "YSGSWGRQ");
		pfXzKeyMap.put("SBJZFQWCZL", "SBJZFQWCZL");
		pfXzKeyMap.put("CWZTFL", "CWZTFL");
		pfXzKeyMap.put("JCZTFL", "JCZTFL");
		pfXzKeyMap.put("ZHTFL", "ZHTFL");
		pfXzKeyMap.put("DXSTFL", "DXSTFL");
		pfXzKeyMap.put("QTTFL", "QTTFL");
		pfXzKeyMap.put("BRANCH", "BRANCH");
		

		// 初始化头部对应的map
		pfXzKeyMapHead = new HashMap<String, String>();
		pfXzKeyMapHead.put("sn", "sn");
		pfXzKeyMapHead.put("sender", "sender");
		// pfXzKeyMapHead.put("receiver", "receiver");
		pfXzKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		fujianMap = new HashMap<String, Long>();
		
	}

	public void readXml(String projectPath) {
		String templeCode = "";
		if (pfXzKeyMap == null)
			this.initpfXzMap();
		File file = new File(pfXzfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(pfXzKeyMap, filePath,pfXzfeedbackPath, pfXzexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(pfXzKeyMapHead,files[i], pfXzfeedbackPath, pfXzexceptionPath);

						// 把数据部分保存到数据库
						YwYnJzfqwczzPfsnForm form = new YwYnJzfqwczzPfsnForm();
						for (String mapKey : pfXzKeyMap.keySet()) {
							form.setYslNo(parseXMLService.getMapValue(pfXzKeyMap.get("YSL_NO")));
							form.setSqdw(parseXMLService.getMapValue(pfXzKeyMap.get("SQDW")));
							templeCode = parseXMLService.getMapValue(pfXzKeyMap.get("BRANCH"));
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(pfXzKeyMap.get("SQDW")),templeCode);
							if(ywComQyxxForm != null){
								form.setQyid(ywComQyxxForm.getId());
							}

							form.setAddress(parseXMLService.getMapValue(pfXzKeyMap.get("ADDRESS")));
							form.setGcdz(parseXMLService.getMapValue(pfXzKeyMap.get("GCDZ")));
							form.setGcmc(parseXMLService.getMapValue(pfXzKeyMap.get("GCMC")));
							form.setJsdwDz(parseXMLService.getMapValue(pfXzKeyMap.get("JSDWDZ")));
							form.setJsdwLxr(parseXMLService.getMapValue(pfXzKeyMap.get("JSDWLXR")));
							form.setJsdwLxrLxdh(parseXMLService.getMapValue(pfXzKeyMap.get("JSDWLXRLXDH")));
							form.setSgdwGz(parseXMLService.getMapValue(pfXzKeyMap.get("SGDWGZ")));
							form.setSgdwXmjl(parseXMLService.getMapValue(pfXzKeyMap.get("SGDWXMJL")));
							form.setSgdwXmjlLxdh(parseXMLService.getMapValue(pfXzKeyMap.get("SGDWXMJLLXDH")));
							form.setSgdwLxr(parseXMLService.getMapValue(pfXzKeyMap.get("SGDWLXR")));
							form.setSgdwLxrLxdh(parseXMLService.getMapValue(pfXzKeyMap.get("SGDWLXRLXDH")));
							form.setYsgs(parseXMLService.getMapValue(pfXzKeyMap.get("YSGS")));
							form.setYsgsLxr(parseXMLService.getMapValue(pfXzKeyMap.get("YSGSLXR")));
							form.setYsgsLxrLxdh(parseXMLService.getMapValue(pfXzKeyMap.get("YSGSLXRLXDH")));
							form.setYsgsKgrq(parseXMLService.getMapValue(pfXzKeyMap.get("YSGSKGRQ")));
							form.setYsgsWgrq(parseXMLService.getMapValue(pfXzKeyMap.get("YSGSWGRQ")));
							form.setSbjzfqwczl(parseXMLService.getMapValue(pfXzKeyMap.get("SBJZFQWCZL")));
							form.setCwztfl(parseXMLService.getMapValue(pfXzKeyMap.get("CWZTFL")));
							form.setJcztfl(parseXMLService.getMapValue(pfXzKeyMap.get("JCZTFL")));
							form.setZhtfl(parseXMLService.getMapValue(pfXzKeyMap.get("ZHTFL")));
							form.setDxstfl(parseXMLService.getMapValue(pfXzKeyMap.get("DXSTFL")));
							form.setQttfl(parseXMLService.getMapValue(pfXzKeyMap.get("QTTFL")));

							form.setBranch(templeCode);
							form.setApprovalNo(parseXMLService.getMapValue(pfXzKeyMap.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(pfXzKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(pfXzKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setYsl(1l);
							form.setSendNotice("Y");// 是否发送通知书到窗口？Y，发送；其他，不发送
						}
						
						// 调用DAO，保存到数据库
						try {
							if("jzfqwczzpf_xz".equals(templeCode)){
								fujianMap = ConfigFjForPf.getPfXzfujianMap(fujianMap);
								ywYnJzfqwczzPfsnService.save(form);
							}else if("jzfqwczzpf_bg".equals(templeCode)){
								fujianMap = ConfigFjForPf.getPfBgfujianMap(fujianMap);
								ywYnJzfqwczzPfsnbgService.save(form);
							}else if("jzfqwczzpf_yq".equals(templeCode)){
								fujianMap = ConfigFjForPf.getPfYqfujianMap(fujianMap);
								ywYnJzfqwczzPfsnyqService.save(form);
							}else if("jzfqwczzpfqx_xz".equals(templeCode)){
								fujianMap = ConfigFjForPf.getPfQxYqfujianMap(fujianMap);
								ywYnJzfqwczzPfsnQxService.save(form);
							}else if("jzfqwczzpfqx_bg".equals(templeCode)){
								fujianMap = ConfigFjForPf.getPfQxYqfujianMap(fujianMap);
								ywYnJzfqwczzPfsnbgQxService.save(form);
							}else if("jzfqwczzpfqx_yq".equals(templeCode)){
								fujianMap = ConfigFjForPf.getPfQxYqfujianMap(fujianMap);
								ywYnJzfqwczzPfsnyqQxService.save(form);
							}
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									pfXzKeyMapHead, pfXzfeedbackPath, pfXzexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, pfXzfujianpath, pfXzfeedbackPath, pfXzexceptionPath, pfXzKeyMapHead,
								files, fujianMap, i, form.getQyid(), form.getId(), templeCode);
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								pfXzKeyMapHead, pfXzfeedbackPath, pfXzexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, pfXzbackPath + fileName,fileName);
				}
			}
		}
	}
}
